<template>
  <div class="loginContainer">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img alt="" src="../assets/logo.png"/>
      </div>
      <!-- 表单区域 -->
      <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginFormRules"
          class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              prefix-icon="iconfont icon-3702mima"
              type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="primary" @click="showDialog">注册</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 修改用户的对话框 -->
    <el-dialog
        :visible.sync="dialogVisible"
        title="用户注册"
        width="50%"
        @close="dialogClosed"
    >
      <!-- 内容主题区域 -->
      <el-form
          ref="formRef"
          :model="addForm"
          :rules="addFormRules"
          label-position='left'
          label-width="100px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="register">注册</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //这是登录表单的数据绑定对象
      loginForm: {
        username: "",
        password: "",
      },
      //这是表单的验证规则对象
      loginFormRules: {
        username: [
          //验证用户名是否合法
          {required: true, message: '请输入登录昵称', trigger: 'blur'}, //失去焦点触发
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          {required: true, message: '请输入登录密码', trigger: 'blur'}, //失去焦点触发
          {
            min: 3,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur',
          },
        ],
      },
      dialogVisible: false,
      //添加用户的表单数据
      addForm: {
        username: "",
        password: ""
      },
      // 添加表单的验证规则对象
      addFormRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '用户名的长度在4~10个字符之间', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ]
      },
    }
  },

  methods: {
    // 用户注册
    showDialog() {
      this.dialogVisible = true
    },

    async login() {
      const res = await this.$http.post('user/login', this.loginForm)

      if (res.code === 200) {
        // 登录成功  存储用户信息
        window.localStorage.setItem('userInfo', JSON.stringify(res.data))
        this.$message.success(res.message)
        return this.$router.push('/home')
      }

      this.$message.error(res.message)
    },

    register() {
      this.$refs.formRef.validate(async (isValid) => {
        if (!isValid) return
        const res = await this.$http.post('user/register', this.addForm) //解构  理解方法: data命名为res

        if (res.code !== 200) return this.$message.error(res.message)

        this.$message.success(res.message)

        this.dialogVisible = false
      })
    },

    dialogClosed() {
      this.$refs.formRef.resetFields()
    },
  },
}
</script>

<style lang="less" scoped>
.loginContainer {
  background-color: #59f3b0;
  height: 100%;
  //div块级元素宽度肯定是100%
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%); //居中显示的方案
    .avatar_box {
      width: 130px;
      height: 130px;
      border-radius: 50%; //为了显示出边框
      border: 1px solid #eee;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  box-sizing: border-box;
  width: 100%;
  padding: 0 20px;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>